package com.songcha.library_common.ui.view

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.view.Gravity
import android.widget.TextView
import com.songcha.library_common.R
import com.songcha.library_common.util.ColorUtil
import com.songcha.library_common.util.dp2px
import java.lang.Float.max
import kotlin.math.max

@SuppressLint("AppCompatCustomView")
class MsgTipsView : TextView {
    var mNum=0
    //var mWidth=0f
    //var mHeight=0f
    var mTextColor=Color.WHITE
    var mDefaultSize=24f.dp2px()
    var mBackgroundColor = Color.RED

   // companion object{
        //val DEFAULT_WIDTH=24.dp2px()
        //val DEFAULT_RECT_WIDTH=48f.dp2px()
  //  }


    constructor(context: Context) : super(context) {
        initView(context,null)
    }

    constructor(context: Context, attrs: AttributeSet):this(context,attrs,0){

    }

    constructor(context: Context, attrs: AttributeSet, defStyle:Int):this(context,attrs,defStyle,0){

    }

    constructor(context: Context, attrs: AttributeSet, defStyle:Int, defRes:Int):super(context,attrs,defStyle,defRes){
        initView(context,attrs)
    }

    private fun initView(context: Context, attrs: AttributeSet?){
        includeFontPadding=false

        if(attrs!=null){
            val ta=context.obtainStyledAttributes(attrs, R.styleable.MsgTipsView)
            mNum=ta.getInt(R.styleable.MsgTipsView_tips_num,0)
            //mWidth=ta.getDimension(R.styleable.MsgTipsView_tips_width,0f)
            //mHeight=ta.getDimension(R.styleable.MsgTipsView_tips_height,0f)
            mDefaultSize=ta.getDimension(R.styleable.MsgTipsView_tips_default_size,24f.dp2px())
            mBackgroundColor=ta.getColor(R.styleable.MsgTipsView_tips_background_color,Color.RED)
            mTextColor=attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android","textColor",Color.WHITE)
            ta.recycle()
        }

    }

    fun setDefaultSize(size:Float){
        mDefaultSize=size
    }

    fun setNum(num:Int){
        if(mNum==num) return
        mNum=num
        invalidate()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        val widthMode=MeasureSpec.getMode(widthMeasureSpec)
        val widthSize=MeasureSpec.getSize(widthMeasureSpec)
        val heightMode=MeasureSpec.getMode(heightMeasureSpec)
        val heightSize=MeasureSpec.getSize(heightMeasureSpec)

        val isRect=mNum>=10

        if(widthMode==MeasureSpec.EXACTLY && heightMode==MeasureSpec.EXACTLY){
            setMeasuredDimension(widthSize,heightSize)
        }else if(widthMode==MeasureSpec.EXACTLY){
            if(isRect){
                setMeasuredDimension(widthSize*2,widthSize)
            }else{
                setMeasuredDimension(widthSize,widthSize)
            }
        }else if(heightMode==MeasureSpec.EXACTLY){
            if(isRect){
                setMeasuredDimension(heightSize*2,heightSize)
            }else{
                setMeasuredDimension(heightSize,heightSize)
            }
        }else{
            if(isRect){
                setMeasuredDimension((mDefaultSize*2).toInt(),(mDefaultSize).toInt())
            }else{
                setMeasuredDimension((mDefaultSize).toInt(),(mDefaultSize).toInt())
            }
        }
    }



    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
        setDrawable()
    }


    private fun setDrawable(){
        //if(mNum==0) return
        val num=mNum.let {
            if(it>99) 99
            else it
        }
        if(num>0){
            text = num.toString()
        }else{
            text=""
        }

        setTextColor(mTextColor)
        gravity=Gravity.CENTER

        val gd=GradientDrawable()
        gd.setColor(mBackgroundColor)

        if(measuredWidth>measuredHeight){
            gd.shape=GradientDrawable.RECTANGLE
            gd.cornerRadius=(measuredWidth/2.0f)
        }else{
            gd.shape=GradientDrawable.OVAL
        }

        gd.setSize(measuredWidth,measuredHeight)

        this.background=gd


    }





}